all repos — website @ cb71f37312622ddf51233629ef0486c7dee716f8

My official website.

src/pages/blog/[...page].astro (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
---
import BlogCard from '@/components/BlogCard.astro'
import Breadcrumbs from '@/components/Breadcrumbs.astro'
import Container from '@/components/Container.astro'
import PaginationComponent from '@/components/ui/pagination'
import { SITE } from '@/consts'
import Layout from '@/layouts/Layout.astro'
import type { PaginateFunction } from 'astro'
import { type CollectionEntry, getCollection } from 'astro:content'

export async function getStaticPaths({
  paginate,
}: {
  paginate: PaginateFunction
}) {
  const allPosts = await getCollection('blog', ({ data }) => !data.draft)
  return paginate(
    allPosts.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf()),
    { pageSize: SITE.POSTS_PER_PAGE },
  )
}

const { page } = Astro.props

const postsByYear = page.data.reduce(
  (acc: Record<string, CollectionEntry<'blog'>[]>, post) => {
    const year = post.data.date.getFullYear().toString()
    ;(acc[year] ??= []).push(post)
    return acc
  },
  {},
)

const years = Object.keys(postsByYear).sort((a, b) => parseInt(b) - parseInt(a))
---

<Layout title="Blog" description="Blog">
  <Container class="flex grow flex-col gap-y-6">
    <Breadcrumbs
      items={[
        { label: 'Blog', href: '/blog', icon: 'lucide:archive' },
        { label: `Page ${page.currentPage}`, icon: 'lucide:folder-open' },
      ]}
    />

    <div class="flex min-h-[calc(100vh-18rem)] flex-col gap-y-8">
      {
        years.map((year) => (
          <section class="flex flex-col gap-y-4">
            <div class="font-semibold">{year}</div>
            <ul class="not-prose flex flex-col gap-4">
              {postsByYear[year].map((post) => (
                <li>
                  <BlogCard entry={post} />
                </li>
              ))}
            </ul>
          </section>
        ))
      }
    </div>

    <PaginationComponent
      currentPage={page.currentPage}
      totalPages={page.lastPage}
      baseUrl="/blog/"
      client:load
    />
  </Container>
</Layout>